<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="编码"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-input v-decorator="['code', {rules: [{required: true, message: '请输入指标编码!'}]}]" />
        </a-form-item>
        <a-form-item
          label="名称"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入指标名称!'}]}]" />
        </a-form-item>
        <a-form-item
          label="父级"
          :labelCol="{span: 4}"
          :wrapperCol="{span: 20}"
        >
          <a-select
            style="width: 100%"
            show-search
            :filter-option="filterOption"
            placeholder="请选二级指标"
            v-decorator="['parentId', {rules: [{required: true, message: '请选二级指标!'}]}]"
          >
            <a-select-option v-for="s in sedData" :value="s.id" :key="s.id">
              {{ s.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>


<script>
import pick from 'lodash.pick'
  import {
    postAxiosUrl,
    getUrl
  } from '@/api/manage'
export default {
  data () {
    return {
      id : '',
      checked : false,
      visible: false,
      confirmLoading: false,
      sedData : [],
      form: this.$form.createForm(this),
    }
  },
  created () {
    this.getSedType()
  },
  methods: {
  add () {
    this.id = ''
    this.form.resetFields()
    this.visible = true
   
  },
  edit (record) {
     const { form: { setFieldsValue } } = this
     this.visible = true
     const me = this
      this.$nextTick(() => {
      me.id = record.id
      setFieldsValue(pick(record, ['name']))
      setFieldsValue(pick(record, ['code']))
       setFieldsValue(pick(record, ['parentId']))
    })
    
  },
  close () {
      this.$emit('close')
      this.visible = false
  },
  handleCancel () {
      this.close()
  },
  handleOk () {
      const _this = this
      
      // 触发表单验证
      this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log('form values', values)
          _this.confirmLoading = true
         values.id = this.id
         postAxiosUrl('cutSedSetting/save', values)
           .then(data => {
              if(!data.success){
                 _this.$message.warning(data.error)
                 _this.confirmLoading = false
               }else{
                 _this.visible = false
                 _this.confirmLoading = false
                 _this.$emit('ok', values)
               }           
             })
        }
      })
    },
    filterOption(input, option) {
          return (
            option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
          )
    },
    getSedType(){
      getUrl('/cutFirstSetting/getAll', {settingType : '0'})
        .then(res => {
          const result = res.result
          this.sedData = result
        })
    },
  }
}
</script>

<style>
</style>
